package com.supermap.guangzhou.weifang.activity;

import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Process;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.supermap.guangzhou.weifang.R;
import com.supermap.guangzhou.weifang.fragment.JiandingFragment;
import com.supermap.guangzhou.weifang.fragment.PuchaFragment;
import com.supermap.guangzhou.weifang.listener.ITextBannerItemClickListener;
import com.supermap.guangzhou.weifang.utils.ToastUtil;
import com.supermap.guangzhou.weifang.widget.CustomAlertDialog;
import com.supermap.guangzhou.weifang.widget.RoundImageView;
import com.supermap.guangzhou.weifang.widget.TextBannerView;
import com.yanzhenjie.alertdialog.AlertDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;

public class MainActivity extends AppCompatActivity {

    ActionBarDrawerToggle mDrawerToggle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rb_pucha)
    RadioButton rbPucha;
    @BindView(R.id.rb_jianding)
    RadioButton rbJianding;
    @BindView(R.id.rg_menu)
    RadioGroup rgMenu;
    @BindView(R.id.nav_view)
    NavigationView navigationView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;
    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private List<String> list;
    private TextBannerView tvNotify;
    private RoundImageView icon;
    private View headerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);


        //初始化toolbal和navigationview
        initToolBar();

        headerView = navigationView.getHeaderView(0);

        initNotify();
        initFirstFragment();

        initIcon();

    }

    private void initIcon() {
        icon = headerView.findViewById(R.id.user_icon);
        icon.setImageResource(R.mipmap.user_icon);
    }

    private void initNotify() {

        tvNotify = headerView.findViewById(R.id.tv_notify);

        list = new ArrayList<>();
        list.add("测试消息通知111");
        list.add("测试消息通知222");
        list.add("测试消息通知333");
        list.add("测试消息通知444");

        tvNotify.setDatas(list);

        tvNotify.startViewAnimator();


        tvNotify.setItemOnClickListener(new ITextBannerItemClickListener() {
            @Override
            public void onItemClick(String data, int position) {
                Drawable drawable = null;
                switch (position){
                    case 0:
                        drawable = getResources().getDrawable(R.mipmap.map1);
                        break;
                    case 1:
                        drawable = getResources().getDrawable(R.mipmap.map2);
                        break;
                    case 2:
                        drawable = getResources().getDrawable(R.mipmap.map3);
                        break;
                    case 3:
                        drawable = getResources().getDrawable(R.mipmap.map4);
                        break;
                }

                AlertDialog.Builder builder = AlertDialog.newBuilder(MainActivity.this);
                ImageView imageView = new ImageView(MainActivity.this);
                imageView.setImageDrawable(drawable);
                builder.setView(imageView);
                builder.setTitle("通知").setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
            }
        });


    }

    private void initFirstFragment() {
        Fragment fragment = new PuchaFragment();
        toolbar.setTitle("广州危房安全普查");
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, fragment).commit();

        rbPucha.setChecked(true);
    }


    /**
     * 初始化toolbal和navigationview
     */
    public void initToolBar() {


        //设置toolbar
        setSupportActionBar(toolbar);

        //左上角图标可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if(tvNotify!=null){
                    tvNotify.startViewAnimator();
                }
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if(tvNotify!=null){
                    tvNotify.stopViewAnimator();
                }
            }
        };



        mDrawerToggle.syncState();
        //设置返回键动画
        drawerLayout.setDrawerListener(mDrawerToggle);


        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                drawerLayout.closeDrawer(navigationView);
                ToastUtil.makeText(MainActivity.this,item.getTitle().toString());
                return true;
            }
        });
    }


    @OnCheckedChanged({R.id.rb_pucha, R.id.rb_jianding})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Fragment fragment = null;
        switch (buttonView.getId()) {
            case R.id.rb_pucha:
                if (isChecked) {
                    fragment = new PuchaFragment();
                    toolbar.setTitle("广州危房安全普查");
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, fragment).commit();
                }
                break;
            case R.id.rb_jianding:
                if (isChecked) {
                    toolbar.setTitle("广州危房安全鉴定");
                    fragment = new JiandingFragment();
                    getSupportFragmentManager().beginTransaction().replace(R.id.fl_content, fragment).commit();
                }
                break;
        }


    }


    private long time = 0;

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() < time + 2000) {
            CustomAlertDialog dialog = new CustomAlertDialog(this);
            dialog.setTitle("温馨提示");
            dialog.setMessage("您确定要退出应用吗？");
            dialog.setIsCanceledOnTouchOutside(false);
            dialog.setConfirmOnClickListener(new CustomAlertDialog.AlertDialogOnClickListener() {
                @Override
                public void onDialogClick(CustomAlertDialog customAlertDialog) {
                    dialog.dismiss();
                    Process.killProcess(Process.myPid());
                    System.exit(0);
                }
            });
            dialog.setCancelOnClickListener(new CustomAlertDialog.AlertDialogOnClickListener() {
                @Override
                public void onDialogClick(CustomAlertDialog customAlertDialog) {
                    dialog.dismiss();
                }
            });

            dialog.show();
        } else {
            ToastUtil.makeText(this, "再次点击，将退出应用！");
            time = System.currentTimeMillis();
        }
    }
}